import 'dart:io';

import 'package:android_intent/android_intent.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_tab/widget/gsy_tabbar_widget.dart';

import 'dynamic_page.dart';
import 'first_page.dart';
import 'my_page.dart';
import 'trend_page.dart';

class HomePage extends StatelessWidget {
  static final String sName = "home";

  _renderTab(icon, text) {
    return new Tab(
      child: new Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          new Icon(
            icon,
            size: 16.0,
          ),
          new Text(text)
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> tabs = [
      _renderTab(Icons.tab, "首页"),
      _renderTab(Icons.tab, "新闻"),
      _renderTab(Icons.tab, "视频"),
      _renderTab(Icons.tab, "我的")
    ];

    return WillPopScope(
      onWillPop: () {
        return _dialogExitApp(context);
      },
      child: new GSYTabBarWidget(
        type: GSYTabBarWidget.BOOTOM_TAB,
        tabItems: tabs,
        tabViews: <Widget>[
          new FirstPage(),
          new DynamicPage(),
          new TrendPage(),
          new MyPage(),
        ],
        backgroundColor: Colors.deepOrange,
        indicatorColor: Colors.cyan,
        title: new Text("tabBar"),
      ),
    );
  }

  Future<bool> _dialogExitApp(BuildContext context) async {
    if (Platform.isAndroid) {
      AndroidIntent intent = AndroidIntent(
        action: 'android.intent.action.MAIN',
        category: "android.intent.category.HOME",
      );
      intent.launch();
    }
    return Future.value(false);
  }
}
